//****include header file
	//COMPLETE 1
#include "sdManager.h"

sdManager::sdManager(void)
{
	//COMPLETE 2
	pDS = NULL;
	dsBuffer1 = NULL;
}

sdManager::~sdManager(void)
{
}

/********************************************************************
* init
* Initializes DirectSound
********************************************************************/
bool sdManager::initDS(HWND hwnd)
{
	//COMPLETE 3
	hr = DirectSoundCreate8(NULL, &pDS, NULL);
	if(FAILED(hr))
		return false;

	hr = pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);
	if(FAILED(hr))
		return false;

	bool okay = loadSB(hwnd);
	if(!okay)
		return false;

	return true;
}

/********************************************************************
* shutdown
* Releases the DirectSound interface
********************************************************************/
void sdManager::shutdownDS(void)
{
    //COMPLETE 4
	if(dsBuffer1 != NULL)
	{
		dsBuffer1->Release();
		dsBuffer1 = NULL;
	}
	if(pDS != NULL)
	{
		pDS->Release();
		pDS = NULL;
	}
}
/*******************************************************************
* LoadWaveToSoundBuffer
* Loads a wave file into a DirectSound Buffer
*******************************************************************/
LPDIRECTSOUNDBUFFER  sdManager::LoadWaveToSoundBuffer(HWND wndHandle, std::string wavFilename)
{
	LPDIRECTSOUNDBUFFER apDSBuffer = NULL;
	CWaveFile *wavFile;

	//***create and open a wave file object
	wavFile = new CWaveFile();
	wavFile->Open((char*)wavFilename.c_str(), NULL, WAVEFILE_READ);  //COMPLETE 5
	
	if( wavFile->GetSize() == 0 )
	{
		MessageBox(wndHandle, "invalid file", "ERROR", MB_OK);
		return false;
	}

	DSBUFFERDESC dsbd;
    ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
    dsbd.dwSize          = sizeof(DSBUFFERDESC);
    dsbd.dwFlags         = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN;
    dsbd.dwBufferBytes   = wavFile->GetSize();
    dsbd.guid3DAlgorithm = GUID_NULL;
	dsbd.lpwfxFormat     = wavFile->m_pwfx;

	//***create a sound buffer
	hr = pDS->CreateSoundBuffer(&dsbd, &apDSBuffer, NULL);//COMPLETE 6

	if FAILED (hr)
	{
		MessageBox(NULL, "unable to create sound buffer", "ERROR", MB_OK);
		return NULL;
	}

	VOID*   pDSLockedBuffer      = NULL; // Pointer to locked buffer memory
    DWORD   dwDSLockedBufferSize = 0;    // Size of the locked DirectSound buffer
    DWORD   dwWavDataRead        = 0;    // Amount of data read from the wav file 

	//***lock 
	
	hr = apDSBuffer->Lock(0, wavFile->GetSize(), &pDSLockedBuffer, &dwDSLockedBufferSize, NULL, NULL, 0L); //COMPLETE 7
	if FAILED (hr)
        return NULL;

    //***Reset the wave file to the beginning 
			//COMPLETE 8
	wavFile->ResetFile();

	//***Read the wave file
    hr = wavFile->Read((BYTE*)pDSLockedBuffer, dwDSLockedBufferSize, &dwWavDataRead); //COMPLETE 9

	// Check to make sure that this was successful
	if FAILED (hr)
        return NULL;

	// Check to make sure the wav file is not empty
    if( dwWavDataRead == 0 )
    {
        // Wav is blank, so just fill with silence
        FillMemory( (BYTE*) pDSLockedBuffer, 
                    dwDSLockedBufferSize, 
                    (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
    }
    else if( dwWavDataRead < dwDSLockedBufferSize )
    {
	    // Don't repeat the wav file, just fill in silence 
        FillMemory( (BYTE*) pDSLockedBuffer + dwWavDataRead, 
                   dwDSLockedBufferSize - dwWavDataRead, 
                   (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
    }

    //*** Unlock the buffer, we don't need it anymore.
    apDSBuffer->Unlock(pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0); //COMPLETE 10

	//*** Clean up
			 //COMPLETE 11
	delete wavFile;

	return apDSBuffer;
}
/*******************************************************************
* LoadSoundBuffer
* Loads a wave file into a DirectSound Buffer
*******************************************************************/
bool sdManager::loadSB(HWND hwnd)
{
	//Load a wave file into a DirectSound buffer
	dsBuffer1 = LoadWaveToSoundBuffer(hwnd, "InClass.wav");
	//Check to make sure that this was successful
	if(dsBuffer1 == NULL)
		return false;

	/*dsBuffer2 = LoadWaveToSoundBuffer(hwnd, "ArrowSht.wav");
	if(dsBuffer2 == NULL)
		return false;*/

	return true;
}
/*******************************************************************
* Play Sound Loop
* Plays a sound from a buffer
*******************************************************************/
void sdManager::playSoundLoop(LPDIRECTSOUNDBUFFER whichBuffer)
{
	whichBuffer->Play( 0, 0, DSBPLAY_LOOPING);
}
/*******************************************************************
* Select a sound buffer to play
* Plays a sound from a buffer
*******************************************************************/
void sdManager::playSound()
{
	//enum {LOOPSOUND, SOUNDEFFECT1, SOUNDEFFECT2};
	//switch(whatSound)
	//{
	//case LOOPSOUND:
	dsBuffer1->SetVolume(-1000);
		dsBuffer1->Play( 0, 0, DSBPLAY_LOOPING);
	//	break;
	/*case SOUNDEFFECT1:
		dsBuffer2->Play( 0, 0, 0);
		break;*/
	//}
}
/*******************************************************************
* Stop Sound
* Stop All Sound
*******************************************************************/
void sdManager::stopSound()
{
	dsBuffer1->Stop();
}